



const { default: axios } = require('axios');
var db = require('../db');
var router = require('express').Router();
var webResult = require('../web-result');


const minecraftPingJava = (...args) => import('@minescope/mineping').then(module => module.pingJava(...args));
const minecraftPingBedrock = (...args) => import('@minescope/mineping').then(module => module.pingBedrock(...args));


router.get("/tool", async (request, response) => {
    response.end(`from ${request.headers.host}`);
});


router.get("/tool/ip", async (request, response) => {
    if (!request.headers.host.includes(":4433"))
    {
        response.header("Content-Type", "text/plain;charset=utf-8");
        response.end("拒绝访问");
        return;
    }
    const ip = request.socket.remoteAddress;
    const port = request.socket.remotePort;
    response.send(webResult.success(`${ip}:${port}`));
});


router.get("/tool/x-real-ip", async (request, response) => {
    const ip = request.headers["x-real-ip"];
    response.send(webResult.success(ip));
});


router.get("/tool/x-forwarded-for", async (request, response) => {
    const ip = request.headers["x-forwarded-for"];
    response.send(webResult.success(ip));
});


router.post("/tool/http", async (request, response) => {
    const { url, headers, body } = request.body;
    let { method, header, data } = request.body;

    if (!url)
    {
        response.send(webResult.failed(undefined, "缺少URL"));
        return;
    }

    if (!method)
        method = "GET";

    // 以 header 为准
    if (headers && !header) {
        header = headers;
      } else if (!headers && !header) {
        header = {};
      } else if (headers && header) {
        Object.assign(header, headers);
      }
  
      // 以 data 为准
      if (body && !data) {
        data = body;
      } else if (!body && !data) {
        data = {};
      } else if (body && data) {
        Object.assign(data, body);
      }

    axios({
        method: method,
        url: url,
        headers: header,
        data: data
    }).then(res => {
        const result = res.data;
        response.send(webResult.success(result));
    }).catch(e => {
        console.error(e);
        e.stack = undefined;
        response.send(webResult.failed(e));
    });
});


router.get("/tool/minecraft/ping/java/:address", async (request, response) => {
    const { address } = request.params;
    minecraftPingJava(address)
    .then(data => {
        response.send(webResult.success(data, "信息获取成功"));
    })
    .catch(_e => {
        // console.error(e);
        response.send(webResult.failed(undefined, "服务不可用"));
    });
});
router.get("/tool/minecraft/ping/bedrock/:address", async (request, response) => {
    const { address } = request.params;
    minecraftPingBedrock(address)
    .then(data => {
        response.send(webResult.success(data, "信息获取成功"));
    })
    .catch(_e => {
        // console.error(e);
        response.send(webResult.failed(undefined, "服务不可用"));
    });
});





module.exports = router;

